package com.ljl;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import java.util.Scanner;

/**
 * @author ljl
 * @version 1.0
 * @date 2022/10/26 9:48
 */
public class ClientHandler extends IoHandlerAdapter {

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        //通道建立后，监听用户的输入
        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextLine()) {
                String s = scanner.nextLine();
                System.out.printf("发送消息[%s]\r\n", s);
                session.write(s);
            }
        }).start();
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        System.out.printf("接收到来自[%s]的消息[%s]\r\n", session.getRemoteAddress().toString(), message);
    }

}
